var appService = angular.module('starter.services', [])
  .factory('CommonUtils', function ($rootScope, $ionicLoading, $timeout, $ionicHistory, $state, $log, $ionicPopup, $ionicViewSwitcher) {
    this.showLoadingDialog = function (msg) {
      $ionicLoading.show({
        template: '<div class="row"><ion-spinner icon="android" class="spinner spinner-android center"></ion-spinner></div><div class="row padding-top"><label>' + msg + '</label></div>'
      });
    };
    this.hideLoadingDialog = function () {
      $ionicLoading.hide();
    };
    this.showToast = function (msg, duration) {
      $ionicLoading.show({
        template: msg
      });
      $timeout(function () {
        $ionicLoading.hide();
      }, duration == null || duration ? 1000 : duration);
    };
    $rootScope.goBack = function () {
      $ionicViewSwitcher.nextDirection('back');
      $ionicHistory.goBack();
    };
    $rootScope.goBackTo = function (state) {
      $ionicViewSwitcher.nextDirection('back');
      $state.go(state);
    };
    $rootScope.goTo = function (state, params) {
      $ionicViewSwitcher.nextDirection('forward');
      $state.go(state, params);
    };
    this.goTo = $rootScope.goTo;
    this.goBackTo = $rootScope.goBackTo;
    this.goBack = $rootScope.goBack;
    return this;
  });
